import { createRouter, createWebHistory } from 'vue-router'
import { staticRoutes } from '@/router/static'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { isLogin } from '@/utils/auth'
import { message } from 'ant-design-vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // history: createWebHashHistory(),
  routes: staticRoutes
})

router.beforeEach((to, from, next) => {
  if (from.path === '/' && !from.name) {
    console.log('刷新了页面')
  }
  NProgress.configure({ showSpinner: false })
  NProgress.start()
  if (to.matched.some((r) => r.meta.requireAuth)) {
    if (isLogin()) {
      next()
    } else {
      message.error('未登录', 1).then(() => message.info('请登录后使用'))
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})

// 路由加载后
router.afterEach(() => {
  NProgress.done()
})

export default router
